<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController {


    public function _initialize()
    {   
      parent::_initialize();

        $team['invite_id']=session('user.id');
         $teamNum=M('user')->where($team)->count();
           $this->assign('teamNum',$teamNum);
               

    }

    public function index(){
            //个人邀请链接
          $invite_url= $_SERVER['HTTP_HOST'].'/index.php/Login/register?invite='.base64_encode($_SESSION['user']['id']);
          $invite_code=base64_encode($_SESSION['user']['id']);

          $invite_code=str_replace("=","",$invite_code);
           $map['user_id']=session('user.id');

            //个人业绩
            $orderNum=M('user')->where('id='.$_SESSION['user']['id'])->field('ordernums')->find();
            $gz_orderNum=M('order_gz')->where($map)->count(); 
            $firstNotice=M('notice')->order('add_time desc')->find();
               $map2['id']=session('user.id');

      $user=M('user')->where($map2)->find(); 
 $jb=M('jbset')->find();
              switch ($user['status']) {
          case '1':
          /* $con='邀请'.$jb['sj_jb2_jb1s'].'个及以上'.$jb['jb1'].','.$jb['sj_jb2_mons'].'个月累计'.($jb['sj_jb2_orders']*$jb['sj_jb2_mons']).' 或者 邀请'.$jb['sj_jb2_jb1s'].'个及以上'.$jb['jb1'].',连续'.$jb['sj_jb2_mons'].'个月每月'.$jb['sj_jb2_orders'].'单及以上';*/
        $con='邀请'.$jb['sj_jb2_jb1s'].'个及以上'.$jb['jb1'].','.$jb['sj_jb2_mons'].'个月累计'.($jb['sj_jb2_orders']*$jb['sj_jb2_mons']);
     
           $this->assign('jb',$jb['jb1']);
              break;
        case '2':
/*            $con='邀请'.$jb['sj_jb3_jb2s'].'个及以上'.$jb['jb2'].','.$jb['sj_jb3_mons'].'个月累计'.($jb['sj_jb3_orders']*$jb['sj_jb3_mons']).' 或者 邀请'.$jb['sj_jb3_jb2s'].'个及以上'.$jb['jb2'].',连续'.$jb['sj_jb3_mons'].'个月每月'.$jb['sj_jb3_orders'].'单及以上';*/
       $con='邀请'.$jb['sj_jb3_jb2s'].'个及以上'.$jb['jb2'].','.$jb['sj_jb3_mons'].'个月累计'.($jb['sj_jb3_orders']*$jb['sj_jb3_mons']);
             
            $this->assign('jb',$jb['jb2']);
              break;
        case '3':
          /*  $con='邀请'.$jb['sj_jb4_jb3s'].'个及以上'.$jb['jb3'].','.$jb['sj_jb4_mons'].'个月累计'.($jb['sj_jb4_orders']*$jb['sj_jb4_mons']).' 或者 邀请'.$jb['sj_jb4_jb3s'].'个及以上'.$jb['jb3'].',连续'.$jb['sj_jb4_mons'].'个月每月'.$jb['sj_jb4_orders'].'单及以上';*/

      $con='邀请'.$jb['sj_jb4_jb3s'].'个及以上'.$jb['jb3'].','.$jb['sj_jb4_mons'].'个月累计'.($jb['sj_jb4_orders']*$jb['sj_jb4_mons']);
            
            $this->assign('jb',$jb['jb3']);
              break;
        case '4':
            $con='您的身份已经是分公司，无法继续升级';
              break;
          
          default:
              # code...
              break;
      }


           $status= parent::$user['status'];
          
           $jb_index='jb'.$status;
           $jibie=$jb[$jb_index];
            $this->assign('firstNotice',$firstNotice['info']);
            $this->assign('index',1);
            $this->assign('jibie',$jibie);
            $this->assign('con',$con);
            $this->assign('invite_code',$invite_code);
            $this->assign('orderNum',$orderNum['ordernums']);
            $this->assign('gz_orderNum',$gz_orderNum);
            $this->assign('invite_url',$invite_url);
           $this->display();
        
    }

   
    
    //系统公告
     public function notice(){
        $notice = D('notice');
        $count = $notice -> count();
        $p = getpage($count, C('PAGE_SIZE'));
        $show = $p -> show();
        $list = $notice -> page(I('get.p')) -> order('id desc') -> limit(C('PAGE_SIZE')) -> select();
        $this -> assign('page', $show);
        // 赋值分页输出
        $this -> assign('list', $list);
        $this -> assign('count', $count);
        $this->assign('notice_nav',1);
        $this->display();
    }


 
   

  


  
    //获取家族结构
    public function getJsonTree()
    {
        $userid=session('user.id');
        $lists=$this->getTeamList($userid);
          $tree = $this->subtree($lists,$userid,1);

foreach($tree as $v) {
    echo str_repeat('&nbsp',$v['lev']),$v['truename'].'&nbsp&nbsp用户id:'.$v['id'],'<br />';
}
      
      
    }



     public function getTeamList($pid=0,$arr=array())
    {
      $map['invite_id']=$pid;
      $team_lists=M('user')->where($map)->field('id,truename,invite_id')->select();
      if ($team_lists) {
        foreach ($team_lists as $key => $value) {
             $team_lists = array_merge($team_lists,$this->getTeamList($value['id'],$team_lists ));
          
        }
        
      }
       
      return $team_lists;
    }

    //递归，查找子孙树
  public  function subtree($arr,$id=0,$lev=1) {
    $subs = array(); // 子孙数组
    foreach($arr as $v) {
      //var_dump($v);
        if($v['invite_id'] == $id) {
            $v['lev'] = $lev;
            $subs[] = $v; // 举例说找到array('id'=>1,'name'=>'安徽','parent'=>0),
            $subs = array_merge($subs,$this->subtree($arr,$v['id'],$lev+1));
        }
    }
    return $subs;
}


public function getChilds()
{ 
      $map['invite_id']=I('post.userid');
      $team_lists=M('user')->where($map)->field('id,truename,invite_id')->select();
      if ($team_lists) {
        foreach ($team_lists as $key => $value) {
          $xj_num=M('user')->where('invite_id='.$value['id'])->count();
          $jsonlist[$key]['text']=$value['truename'];
            if ($xj_num>0) {
              $jsonlist[$key]['children']=$value['truename'];
            }


              }
      }

     echo json_encode($team_lists);
}







}
